﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="DocumentHasUnexpectedTypeException.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Code.Analysis.StyleCop.Rules.Exceptions
{
    #region Usings

    using System;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Security.Permissions;

    using ExulLibrary.Code.Analysis.StyleCop.Rules.Properties;

    #endregion

    /// <summary>
    ///     Describes an error when StyleCop document has unexpected.
    /// </summary>
    [Serializable]
    public sealed class DocumentHasUnexpectedTypeException : ArgumentException
    {
        #region Constructors and Destructors

        #region public

        /// <summary>
        ///     Initializes a new instance of the <see cref="DocumentHasUnexpectedTypeException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="actualDocumentType">
        ///     The actual type of document.
        /// </param>
        /// <param name="expectedDocumentType">
        ///     The expected type of document.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="actualDocumentType"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="expectedDocumentType"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        public DocumentHasUnexpectedTypeException(string paramName, Type actualDocumentType, Type expectedDocumentType)
            : this(paramName, actualDocumentType, expectedDocumentType, null)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="DocumentHasUnexpectedTypeException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="actualDocumentType">
        ///     The actual type of document.
        /// </param>
        /// <param name="expectedDocumentType">
        ///     The expected type of document.
        /// </param>
        /// <param name="innerException">
        ///     The inner exception.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="actualDocumentType"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="expectedDocumentType"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        public DocumentHasUnexpectedTypeException(
            string paramName, Type actualDocumentType, Type expectedDocumentType, Exception innerException)
            : base(Resources.DocumentHasUnexpectedType, paramName, innerException)
        {
            if (actualDocumentType == null)
            {
                throw new ArgumentNullException("actualDocumentType");
            }

            if (expectedDocumentType == null)
            {
                throw new ArgumentNullException("expectedDocumentType");
            }

            this.ActualDocumentType = actualDocumentType;
            this.ExpectedDocumentType = expectedDocumentType;
        }

        #endregion

        #region private

        /// <summary>
        ///     Initializes a new instance of the <see cref="DocumentHasUnexpectedTypeException"/> class.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        private DocumentHasUnexpectedTypeException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this.ActualDocumentType = (Type)info.GetValue("ActualDocumentType", typeof(Type));
            this.ExpectedDocumentType = (Type)info.GetValue("ExpectedDocumentType", typeof(Type));
        }

        #endregion

        #endregion

        #region Properties

        #region public

        /// <summary>
        ///     Gets the actual type of document.
        /// </summary>
        /// <value>
        ///     The actual type of document.
        /// </value>
        public Type ActualDocumentType
        {
            get;
            private set;
        }

        /// <summary>
        ///     Gets the expected type of document.
        /// </summary>
        /// <value>
        ///     The expected type of document.
        /// </value>
        public Type ExpectedDocumentType
        {
            get;
            private set;
        }

        #endregion

        #endregion

        #region Overridden Properties

        #region public

        /// <summary>
        ///     Gets the error message and the parameter name, or only the error message if no parameter name is set.
        /// </summary>
        /// <value>
        ///     A text string describing the details of the exception.
        /// </value>
        public override string Message
        {
            get
            {
                return string.Format(
                    CultureInfo.CurrentCulture,
                    base.Message,
                    this.ParamName,
                    this.ExpectedDocumentType.FullName,
                    this.ActualDocumentType.FullName);
            }
        }

        #endregion

        #endregion

        #region Overridden Methods

        #region public

        /// <summary>
        ///     Sets the <see cref="SerializationInfo"/> object with the parameter name and additional exception information.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="info"/> is <see langword="null"/>.
        /// </exception>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            info.AddValue("ActualDocumentType", this.ActualDocumentType);
            info.AddValue("ExpectedDocumentType", this.ExpectedDocumentType);
        }

        #endregion

        #endregion
    }
}